<?php
/**
 * 用户注册，登录类
 * @author 高文杰 <gaowenjie_xy@126.com>
 */
import("@.ORG.Url");
import("@.ORG.Validation");

class LoginAction extends Action {
    //用户登录
    public function login()
    {
        //如果用户已经登录，则直接跳转到首页.
        if (session('u_name'))
        {
            $this->redirect("Index/index");
        }

        if ($this->isPost())
        {
            if ($this->_post('username') && $this->_post('userpass'))
            {
                $islogin = LoginModel::loginAuth($this->_post('username'), $this->_post('userpass'));

                if ($islogin)
                {
                    //修改最后登录的时间.
                    $this->redirect("Index/index");
                }
                else
                {
                    $this->error("请确认用户名和密码...");
                }
            }
            else
            {
                $this->error("请输入用户名和密码...");
            }
        }

        $this->display();
    }

    //退出
    public function logout(){
        //正常退出，记录退出时间.
        UserLoginModel::recordLogoutLog();

        //销毁SESSION.
        LoginModel::destroySession();
        $this->success("退出成功", "../index");
    }

    /**
     * 注册用户
     */
    public function register()
    {
        if ($this->isPost())
        {
            if ($this->_post('userName') && $this->_post('userPassword'))
            {
                if (md5($this->_post('userPassword')) == md5($this->_post('userCheckPassword')))
                {
                    // 注册账户
                    if (userModel::checkUser($this->_post("userName")))
                    {
                        $this->error("该用户已存在！");
                    }

                    $iUid = userModel::register($this->_post('userName'), $this->_post('userPassword'), $this->_post("email"));

                    if ($iUid > 0)
                    {
                        // 发送用户激活邮件
                        EmailAction::sendEmail($this->_post('email'), '10网络激活邮件', self::authEmail($iUid ));
                        $this->success('用户注册成功,赶紧去登录吧！', "login");
                    }
                    else
                    {
                        $this->error("用户注册失败，请与管理员联系.");
                    }
                }
                else
                {
                    $this->error("两次密码不一致，请重新确认!");
                }
            }
            else
            {
                $this->error("用户名和密码不能为空。");
            }
        }

        $this->display();
    }

    /**
     * 验证邮箱参数加密
     */
    public static function authEmail($id)
    {
        $url = WEB_SITE_PATH . "/Login/checkEmail/uid/" . base64_encode($id).'/auth/'.base64_encode('1');
        $sEmailContent = "请点击该链接验证邮箱<a target='_blank' href='{$url}'>验证邮箱</a>,如果该链接无法打开，请将该链接粘贴到浏览器中验证，如果已经验证成功，请忽略此次验证！";
        return $sEmailContent;
    }

    /**
     * 用户验证邮箱
     *
     */
    public function checkEmail()
    {
        $uId = base64_decode($this->_get("uid"));
        $auth = base64_decode($this->_get("auth"));

        if (empty($uId) || empty($auth)) {

            $this->error("邮箱验证失败！", "login");
        }

        // 修改邮箱状态
        $r = userModel::checkEmail($uId, $auth);

        if ($r == 0) {
            $this->success("邮箱验证成功！", "login");
        } else {
            $this->error("邮箱验证失败！", "login");
        }
    }

    /**
     * 忘记密码
     */
    public function forget()
    {
        if ($this->isPost()) {
            $userName = $this->_post('username');
            $email = $this->_post("email");

            if (empty($userName) || empty($email)) {
                $this->error("用户名和邮箱不能为空！");
            }

            $isForget = LoginModel::forget($userName, $email);

            if ($isForget) {
                $this->success("邮箱验证已发出，请登录邮箱修改密码！");
            } else {
                $this->error("找寻密码失败，请联系管理人员！");
            }
        }

        $this->display();
    }
}
?>